<?php

// API 接口路由， https://www.kancloud.cn/manual/thinkphp6_0/1037495

use think\facade\Route;

// 路由组
Route::group('api-v1/', function(){
    // 访问 domain/api-v1/version
    Route::get('version', function(){
        return 'this is v1 version api.';
    });

    // 资源路由，https://www.kancloud.cn/manual/thinkphp6_0/1037501
    // 资源控制器,https://www.kancloud.cn/manual/thinkphp6_0/1037514
    // php think make:controller api/v1/TestController --api
    Route::resource('test', \app\controller\api\v1\TestController::class);

    /*
     * 中间件的路由 demo
     * app/middleware/TestBeforeMiddleware.php
     * app/middleware/TestAfterMiddleware.php
     * config/middleware.php
     * */
    Route::get('test-middleware', \app\controller\api\v1\TestMiddlewareController::class.'/index')
        ->name('index')
        ->middleware(['test-before', 'test-after']);

    /*
     * 内置规则，https://www.kancloud.cn/manual/thinkphp6_0/1037629
     *
     * Validate demo
     * Validate 基类，vendor/topthink/framework/src/think/facade/Validate.php
     * */
    Route::resource('test-validate', \app\controller\api\v1\TestValidateController::class);

    // 会员-登录
    Route::post('user/login', \app\controller\api\v1\UserController::class.'/login')->name('login');
    // 会员-注册
    Route::post('user/register', \app\controller\api\v1\UserController::class.'/register')->name('register');
})->allowCrossDomain([
    // 跨域配置，https://www.kancloud.cn/manual/thinkphp6_0/1037507
    'Access-Control-Allow-Origin'   => '*',
    'Access-Control-Allow-Methods'  => 'GET, POST, PATCH, PUT, DELETE',
    'Access-Control-Allow-Headers'  => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With'
])->middleware('throttle');